package com.naizhuai.ui.components;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.naizhuai.R;
import com.naizhuai.util.ScreenUtils;

import java.util.List;

/**
 * @author：lichenxi
 * @date 2018/4/30 18
 * email：525603977@qq.com
 * Fighting
 */
public class MarqueeTextView extends LinearLayout {
    //textcolor textsize
    private ViewFlipper mViewFlipper;

    private  int textColor=R.color.gray_light;

    public MarqueeTextView(Context context) {
        this(context,null);
    }

    public MarqueeTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,-1);
    }
  private ClickCallBack mClickCallBack;

    public void setClickCallBack(ClickCallBack clickCallBack) {
          mClickCallBack = clickCallBack;
    }

    public MarqueeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        initBaseView();
    }

    private void initBaseView() {
        mViewFlipper=new ViewFlipper(getContext());
        LayoutParams layoutParams=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mViewFlipper.setLayoutParams(layoutParams);
        addView(mViewFlipper);
        mViewFlipper.setInAnimation(setInAnimation());
        mViewFlipper.setOutAnimation(setOutAnimation());
        mViewFlipper.setFlipInterval(2000);
        mViewFlipper.startFlipping();
    }
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public void initTextDatas(List<String> list){
        mViewFlipper.removeAllViews();
        if (list.size()==0){
            return;
        }
        int size = list.size();
        for (int i = 0; i < size; i++) {
            final  int j=i;
            String string=list.get(j);
            TextView textView=new TextView(getContext());
            LayoutParams layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            layoutParams.gravity = Gravity.CENTER;
            textView.setMaxLines(1);
            textView.setMaxEms(8);
            textView.setEllipsize(TextUtils.TruncateAt.END);
            textView.setLayoutParams(layoutParams);
            textView.setTextColor(getResources().getColor(textColor));
            textView.setPadding(ScreenUtils.px2dp(getContext(),20),0,0,0);
            textView.setGravity(Gravity.CENTER_VERTICAL);
            textView.setText(string);
            textView.setOnClickListener(v -> mClickCallBack.clickPostion(j));
            mViewFlipper.addView(textView);
        }


    }
    public TranslateAnimation setInAnimation(){
         TranslateAnimation animation=new TranslateAnimation(0,0,100,0);
         animation.setDuration(1000);
         return animation;
    }
    public TranslateAnimation setOutAnimation(){
        TranslateAnimation animation=new TranslateAnimation(0,0,0,-100);
        animation.setDuration(1000);
        return animation;
    }
    public void recycle(){
        if (mViewFlipper!=null){
           if (mViewFlipper.isFlipping()){
               mViewFlipper.stopFlipping();
           }
            mViewFlipper.removeAllViews();
        }
        mViewFlipper=null;
    }

     public interface  ClickCallBack{
         void clickPostion(int position);
   }
}

